package com.zpc.myvideoserver.service;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.DownloadUrl;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.zpc.myvideoserver.entity.FileMessage;
import com.zpc.myvideoserver.entity.Video;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : GetFileInfoService  //类名
 * @Description : 获取文件信息服务类  //描述
 * @Author : zpc20 //作者
 * @Date: 2023/10/25  20:36
 */

@Service
public class GetFileInfoService {
    @Value("${qiNiu.bucketName}")
    private String bucketName;
    @Value("${qiNiu.domain}")
    private  String domain;

    @Value("${qiNiu.root_path}")
    private  String rootPath;

    @Resource
    private BucketManager bucketManager;
    @Resource
    private  Auth auth;

    private String getUrl(String fileName, long expireInSeconds){
        DownloadUrl url = new DownloadUrl(domain,false, rootPath+fileName);
        // @Description : 生成Url zpc20 2023/11/6 8:22
        try {
            long deadline = System.currentTimeMillis()/1000 + expireInSeconds;
            String urlString = url.buildURL(auth, deadline);
            return urlString;
        } catch (QiniuException e) {
            throw new RuntimeException(e);
        }
    }

    public Map<String,Object> GetVideoFiles(Video video, long expireInSeconds){

        Map<String,Object> urlmap = new HashMap<>();
        urlmap.put("videoUrl",getUrl("video/"+video.getVideoName(),expireInSeconds));
        urlmap.put("coverUrl",getUrl("cover/"+video.getCoverName(),expireInSeconds));
        return urlmap;
    }
    public  FileMessage GetFileUrl(String fileName,long expireInSeconds){
        // Auth auth = Auth.create(accessKey, secretKey);
        long deadline = System.currentTimeMillis()/1000 + expireInSeconds;
        // Configuration cfg = new Configuration(Region.huanan());
        FileMessage fileMessage = new FileMessage();
        try {
            // @Description : 获取信息 zpc20 2023/11/6 8:22
            FileInfo fileInfo = bucketManager.stat(bucketName, "myvideo/"+fileName);
            // @Description : 获取下载链接 zpc20 2023/11/6 8:25
            DownloadUrl url = new DownloadUrl(domain,false, fileName);
            // @Description : 生成Url zpc20 2023/11/6 8:22
            String urlString = url.buildURL(auth, deadline);
            fileMessage.setFileName(fileName);
            fileMessage.setFileSize(fileInfo.fsize);
            fileMessage.setFileUrl(urlString);
            fileMessage.setDeadline(deadline);
            fileMessage.setInfo(String.valueOf(fileInfo.status));
            return fileMessage;
        }catch (QiniuException ex){
            System.out.println(ex);
            fileMessage.setInfo(ex.error());
            return fileMessage;
        }
    }
    public void GetAllFileInfo() throws QiniuException {
        // 文件名前缀
        String prefix = "";
        int limit = 1000;
        String delimiter = "";
        // 获取文件列表
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucketName, prefix, limit, delimiter);
        while (fileListIterator.hasNext()) {
            FileInfo[] items = fileListIterator.next();
            for (FileInfo item : items) {
                System.out.println(item.key);
                System.out.println(item.hash);
                System.out.println(item.fsize);
                System.out.println(item.mimeType);
                System.out.println(item.putTime);
                System.out.println(item.endUser);
            }
        }
    }


}
